/*
 * Manager.cpp
 *
 *  Created on: 2011/07/02
 *      Author: naoki
 */

#include "NTEngine/NTEngine.h"
#include "NTEngine/Core/Core.h"
#include "Input/src/ManagerImpl.h"

#ifndef NDEBUG
// テスト用
#include <android/log.h>
#ifdef LOGI
#undef LOGI
#endif
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "Input", __VA_ARGS__))
#else
#define LOGI(...)
#endif

namespace NTEngine {
namespace Input {

ManagerImpl* gManagerImpl = 0;

static int32_t engine_handle_input( android_app*, AInputEvent* event ) {
	int32_t ret = 0;
	if ( gManagerImpl ) {
		ret = gManagerImpl->onHandleEvent( event );
	}
	return ret;
}

Manager::Manager() {
	ASSERT( Core::isMainThread() && "you must call from MAIN thread" );
}

Manager Manager::instance() {
	return Manager();
}

void Manager::update() {
	ASSERT( gManagerImpl );
	gManagerImpl->update();
}

Touch Manager::touch() {
	return Touch();
}

Keyboard Manager::keyboard() {
	return Keyboard();
}

void Manager::create( void* p ) {
	HARD_ASSERT( gManagerImpl == 0 );
	android_app* app = static_cast< android_app* >( p );
	gManagerImpl = NEW ManagerImpl( app );
	// 生成が終わったらコールバックを登録する
	app->onInputEvent = engine_handle_input;
}

void Manager::destroy() {
	SAFE_DELETE( gManagerImpl );
}

} // namespace Input
} // namespace NTEngine
